package com.easemob.chatuidemo.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.easemob.applib.model.HXSDKModel;
import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMChatOptions;
import com.easemob.chatuidemo.R;
import com.easemob.chatuidemo.activity.BlacklistActivity;
import com.easemob.chatuidemo.activity.DiagnoseActivity;
import com.easemob.chatuidemo.activity.OfflinePushNickActivity;


/**
 * Created by dllo on 15/9/14.
 */
public class MiscellaneousFunctionFragment extends Fragment implements View.OnClickListener {


    /**
     * 设置扬声器布局
     */
    private RelativeLayout rl_switch_speaker;

    /**
     * 打开扬声器播放语音
     */
    private ImageView iv_switch_open_speaker;
    /**
     * 关闭扬声器播放语音
     */
    private ImageView iv_switch_close_speaker;

    /**
     * 声音和震动中间的那条线
     */
    private TextView textview1, textview2;

    private LinearLayout blacklistContainer;


    private RelativeLayout rl_switch_chatroom_leave;
    private ImageView iv_switch_room_owner_leave_allow;
    private ImageView iv_switch_room_owner_leave_disallow;

    private EMChatOptions chatOptions;

    /**
     * 诊断
     */
    private LinearLayout llDiagnose;
    /**
     * iOS离线推送昵称
     */
    private LinearLayout pushNick;

    HXSDKModel model;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_miscellaneous_function, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (savedInstanceState != null && savedInstanceState.getBoolean("isConflict", false))
            return;

        init();
    }

    /**
     * 初始化
     **/

    public void init() {
        rl_switch_speaker = (RelativeLayout) getView().findViewById(R.id.rl_switch_speaker);
        iv_switch_open_speaker = (ImageView) getView().findViewById(R.id.iv_switch_open_speaker);
        iv_switch_close_speaker = (ImageView) getView().findViewById(R.id.iv_switch_close_speaker);
        rl_switch_chatroom_leave = (RelativeLayout) getView().findViewById(R.id.rl_switch_chatroom_owner_leave);

        iv_switch_room_owner_leave_allow = (ImageView) getView().findViewById(R.id.iv_switch_chatroom_owner_leave_allow);
        iv_switch_room_owner_leave_disallow = (ImageView) getView().findViewById(R.id.iv_switch_chatroom_owner_leave_not_allow);

        textview1 = (TextView) getView().findViewById(R.id.textview1);
        textview2 = (TextView) getView().findViewById(R.id.textview2);
        blacklistContainer = (LinearLayout) getView().findViewById(R.id.ll_black_list);
        llDiagnose = (LinearLayout) getView().findViewById(R.id.ll_diagnose);
        pushNick = (LinearLayout) getView().findViewById(R.id.ll_set_push_nick);

        // 监听事件

        blacklistContainer.setOnClickListener(this);
        rl_switch_speaker.setOnClickListener(this);
        llDiagnose.setOnClickListener(this);
        pushNick.setOnClickListener(this);
        rl_switch_chatroom_leave.setOnClickListener(this);

        chatOptions = EMChatManager.getInstance().getChatOptions();
        model = (HXSDKModel) com.easemob.applib.controller.HXSDKHelper.getInstance().getModel();


        // 是否打开扬声器
        // the speaker is switched on or not?
        if (model.getSettingMsgSpeaker()) {
            iv_switch_open_speaker.setVisibility(View.VISIBLE);
            iv_switch_close_speaker.setVisibility(View.INVISIBLE);
        } else {
            iv_switch_open_speaker.setVisibility(View.INVISIBLE);
            iv_switch_close_speaker.setVisibility(View.VISIBLE);
        }

        // 是否允许聊天室owner leave
        if (model.isChatroomOwnerLeaveAllowed()) {
            iv_switch_room_owner_leave_allow.setVisibility(View.VISIBLE);
            iv_switch_room_owner_leave_disallow.setVisibility(View.INVISIBLE);
        } else {
            iv_switch_room_owner_leave_allow.setVisibility(View.INVISIBLE);
            iv_switch_room_owner_leave_disallow.setVisibility(View.VISIBLE);
        }
    }

    // 监听回调
    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.rl_switch_speaker:
                if (iv_switch_open_speaker.getVisibility() == View.VISIBLE) {
                    iv_switch_open_speaker.setVisibility(View.INVISIBLE);
                    iv_switch_close_speaker.setVisibility(View.VISIBLE);
                    chatOptions.setUseSpeaker(false);
                    EMChatManager.getInstance().setChatOptions(chatOptions);
                    com.easemob.applib.controller.HXSDKHelper.getInstance().getModel().setSettingMsgSpeaker(false);
                } else {
                    iv_switch_open_speaker.setVisibility(View.VISIBLE);
                    iv_switch_close_speaker.setVisibility(View.INVISIBLE);
                    chatOptions.setUseSpeaker(true);
                    EMChatManager.getInstance().setChatOptions(chatOptions);
                    com.easemob.applib.controller.HXSDKHelper.getInstance().getModel().setSettingMsgVibrate(true);
                }
                break;
            case R.id.rl_switch_chatroom_owner_leave:
                if (this.iv_switch_room_owner_leave_allow.getVisibility() == View.VISIBLE) {
                    iv_switch_room_owner_leave_allow.setVisibility(View.INVISIBLE);
                    iv_switch_room_owner_leave_disallow.setVisibility(View.VISIBLE);
                    chatOptions.allowChatroomOwnerLeave(false);
                    EMChatManager.getInstance().setChatOptions(chatOptions);
                    model.allowChatroomOwnerLeave(false);

                } else {
                    iv_switch_room_owner_leave_allow.setVisibility(View.VISIBLE);
                    iv_switch_room_owner_leave_disallow.setVisibility(View.INVISIBLE);
                    chatOptions.allowChatroomOwnerLeave(true);
                    EMChatManager.getInstance().setChatOptions(chatOptions);
                    model.allowChatroomOwnerLeave(true);
                }
                break;

            case R.id.ll_black_list:
                startActivity(new Intent(getActivity(), BlacklistActivity.class));
                break;
            case R.id.ll_diagnose:
                startActivity(new Intent(getActivity(), DiagnoseActivity.class));
                break;
            case R.id.ll_set_push_nick:
                startActivity(new Intent(getActivity(), OfflinePushNickActivity.class));
                break;

            default:
                break;
        }
    }

}
